package com.example.farmer.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.farmer.Presenter.PurchasePostPresenter;
import com.example.farmer.R;
import com.example.farmer.View.Purchase.PurchasePostView;

public class PurchasePublishActivity extends AppCompatActivity implements PurchasePostView {


    private PurchasePostPresenter mPresenter;
    private Spinner spProductType, spPushtype;
    private EditText etProductName, etNeed, etShipaddress, etShippingAddress,
            etperiod, etdescribe;
    private Button btnPublishPurchase;
    private ImageButton backbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_purchase_publish);

        spPushtype = findViewById(R.id.sppushtype);
        spProductType = findViewById(R.id.spProductType);
        etProductName = findViewById(R.id.etProductName);
        etNeed = findViewById(R.id.etNeed);
        etShipaddress = findViewById(R.id.etShipaddress);
        etperiod = findViewById(R.id.etperiod);
        etShippingAddress = findViewById(R.id.etShippingAddress);
        etdescribe = findViewById(R.id.etdescribe);
        btnPublishPurchase = findViewById(R.id.btnPublishPurchase);


        mPresenter = new PurchasePostPresenter(this);


        backbutton = findViewById(R.id.back_button);
        backbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();// 结束当前活动并返回
            }
        });
        // 设置发布按钮的点击事件
        btnPublishPurchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String pushtype = spPushtype.getSelectedItem().toString();
                String producttype = spProductType.getSelectedItem().toString()/* 从UI获取 */;
                String productname = etProductName.getText().toString()/* 从UI获取 */;
                String need =etNeed.getText().toString()/* 从UI获取 */;
                String shipaddress = etShipaddress.getText().toString();/* 从UI获取 */;
                String period =etperiod.getText().toString() /* 从UI获取 */;
                String shippingaddress =etShippingAddress.getText().toString() /* 从UI获取 */;
                String describe =etdescribe.getText().toString() /* 从UI获取 */;
                mPresenter.posts(pushtype,productname,producttype,need,shipaddress, period,
                        shippingaddress,describe);
                publishPurchase();
            }
        });
    }

    @Override
    public void showLoading() {
        // 显示加载指示器
    }

    @Override
    public void hideLoading() {
        // 隐藏加载指示器
    }

    @Override
    public void showPostSuccess() {
        // 发布成功
        Toast.makeText(this, "发布成功", Toast.LENGTH_SHORT).show();
        navigateToNewsFragment();
    }

    @Override
    public void showPostError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void navigateToNewsFragment() {

        finish();// 结束当前活动并返回
    }

    @Override
    public Context getContext() {
        return this;
    }

    private void publishPurchase() {
        String pushtype = spPushtype.getSelectedItem().toString();
        String producttype = spProductType.getSelectedItem().toString()/* 从UI获取 */;
        String productname = etProductName.getText().toString()/* 从UI获取 */;
        String need =etNeed.getText().toString()/* 从UI获取 */;
        String shipaddress = etShipaddress.getText().toString();/* 从UI获取 */;
        String period =etperiod.getText().toString() /* 从UI获取 */;
        String shippingaddress =etShippingAddress.getText().toString() /* 从UI获取 */;
        String describe =etdescribe.getText().toString() /* 从UI获取 */;

        // 在这里添加发布采购的逻辑，例如将数据提交到服务器或本地存储

        // 简单示例：显示发布信息
        String message = "发布采购信息:\n" +
                "发布类型: " + pushtype + "\n" +
                "货品类型: " + producttype + "\n" +
                "货品名称: " + productname + "\n" +
                "需求量: " + need + "\n" +
                "期望货源地: " + shipaddress + "\n" +
                "采购周期: " + period + "\n" +
                "收货地区: " + shippingaddress + "\n" +
                "采购描述: " + describe;
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}